import React from 'react';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import { Card, Form, Input, Button, message, Row, Col, InputNumber, Space, Tabs } from 'antd';
import { connect } from 'umi';

const { TabPane } = Tabs;
class ViewPage extends React.Component<any, any> {
    constructor(props: any) {
        super(props);
        this.state = {
            loading: false,
            datas: {},
        }
    }
    formRef: any = React.createRef();
    componentDidMount() {
        this.getData();
    }
    getData = () => {
        const { dispatch } = this.props;
        dispatch({
            type: 'Abp/getEduConfig',
        }).then(() => {
            var result = this.props.response.result;
            this.setState({ datas: result });
            this.formRef.current.setFieldsValue({ model: result });
        });
    }
    onFinish = async (values: any) => {
        const { datas } = this.state;
        var act = "Abp/create";
        if (datas)
            act = "Abp/update";
        this.props.dispatch({
            type: act,
            payload: values.model,
            control: "Edu_Config",
        }).then(() => {
            var resp = this.props.response;
            if (resp.success) {
                this.setState({ datas: resp.result });
                message.success('保存成功！');
            }
            else {
                message.error('保存错误，请联系管理员');
            }
        });
    };
    handleCode=()=>{
        this.props.dispatch({
            type:'Abp/updateConfig',
            payload:{},
        }).then(()=>{
            var resp = this.props.response;
            if (resp.success) {
                this.setState({ datas: resp.result });
                message.success('验证码校验成功！');
            }
            else {
                message.error('验证码校验错误，请联系管理员');
            }
        })
    }
    render() {
        return (
            <PageHeaderWrapper>
                <Row>
                    <Col span={16}>
                        <Card>
                            <Form ref={this.formRef} layout="vertical" onFinish={this.onFinish} >
                                <Tabs defaultActiveKey="1" >
                                    <TabPane tab="基本" key="1">
                                        <div style={{ display: 'none' }}>
                                            <Form.Item
                                                label="id"
                                                name={['model', 'id']}>
                                                <Input />
                                            </Form.Item>
                                        </div>
                                        <Form.Item
                                            name={['model', 'company']}
                                            label="公司名字"
                                            rules={[{ required: true, message: "请输入公司名字" }]}>
                                            <Input style={{ width: 400 }} />
                                        </Form.Item>
                                        <Form.Item
                                            name={['model', 'address']}
                                            label="联系地址"
                                            rules={[{ required: true, message: "请输入联系地址" }]}>
                                            <Input style={{ width: 400 }} />
                                        </Form.Item>
                                        <Form.Item style={{ marginBottom: 0 }}>
                                            <Input.Group compact>
                                                <Form.Item
                                                    name={['model', 'telphone']}
                                                    label="联系电话">
                                                    <Input />
                                                </Form.Item>
                                                <Form.Item
                                                    name={['model', 'telphoneCS']}
                                                    label="客服电话">
                                                    <Input />
                                                </Form.Item>
                                            </Input.Group>
                                        </Form.Item>
                                        <Form.Item style={{ marginBottom: 0 }}>
                                            <Input.Group compact>
                                                <Form.Item
                                                    name={['model', 'commRate1']}
                                                    label="资源所有者">
                                                    <InputNumber step={0.001} min={0} />
                                                </Form.Item>
                                                <Form.Item
                                                    name={['model', 'commRate2']}
                                                    label="用户分销">
                                                    <InputNumber step={0.001} min={0} />
                                                </Form.Item>
                                                <Form.Item
                                                    name={['model', 'commRate3']}
                                                    label="分销商">
                                                    <InputNumber step={0.001} min={0} />
                                                </Form.Item>
                                            </Input.Group>
                                        </Form.Item>
                                        <Form.Item
                                            name={['model', 'exchgeRate']}
                                            label="积分兑换比例"
                                            rules={[{ required: true, message: "请输入兑换比例" }]}>
                                            <InputNumber step={0.001} min={0} />
                                        </Form.Item>
                                        <Form.Item
                                            name={['model', 'memberGrade']}
                                            label="会员等级积分要求"
                                            rules={[{ required: true }]}>
                                            <Input style={{ width: 400 }} />
                                        </Form.Item>
                                        <Form.Item style={{ marginBottom: 0 }}>
                                            <Input.Group compact>
                                                <Form.Item
                                                    name={['model', 'orderNoPre']}
                                                    label="订单号前缀"
                                                    rules={[{ required: true, message: "请输入订单号前缀" }]}>
                                                    <Input style={{ width: 100 }} />
                                                </Form.Item>
                                                <Form.Item
                                                    name={['model', 'startOrderNo']}
                                                    label="起始订单编号">
                                                    <InputNumber style={{ width: 240 }} min={1000} />
                                                </Form.Item>
                                            </Input.Group>
                                        </Form.Item>
                                        <Form.Item style={{ marginBottom: 0 }}>
                                            <Input.Group compact>
                                                <Form.Item
                                                    name={['model', 'smtP_Server']}
                                                    label="邮局服务器">
                                                    <Input />
                                                </Form.Item>
                                                <Form.Item
                                                    name={['model', 'smtP_Port']}
                                                    label="邮件端口">
                                                    <Input />
                                                </Form.Item>
                                            </Input.Group>
                                        </Form.Item>
                                        <Form.Item style={{ marginBottom: 0 }}>
                                            <Input.Group compact>
                                                <Form.Item
                                                    name={['model', 'smtP_User']}
                                                    label="邮件用户">
                                                    <Input />
                                                </Form.Item>
                                                <Form.Item
                                                    name={['model', 'smtP_Password']}
                                                    label="邮件密码">
                                                    <Input.Password />
                                                </Form.Item>
                                            </Input.Group>
                                        </Form.Item>

                                    </TabPane>
                                    <TabPane tab="APP设置" key="2">
                                        <Form.Item style={{ marginBottom: 0 }}>
                                            <Input.Group compact>
                                                <Form.Item
                                                    name={['model', 'appVersion']}
                                                    label="安卓APP版本号">
                                                    <Input />
                                                </Form.Item>
                                                <Form.Item
                                                    name={['model', 'updateUrl']}
                                                    label="安卓APP更新地址">
                                                    <Input style={{ width: 360 }} />
                                                </Form.Item>
                                            </Input.Group>
                                        </Form.Item>
                                        <Form.Item style={{ marginBottom: 0 }}>
                                            <Input.Group compact>
                                                <Form.Item
                                                    name={['model', 'appVersionIOS']}
                                                    label="苹果APP版本号">
                                                    <Input />
                                                </Form.Item>
                                                <Form.Item
                                                    name={['model', 'updateUrlIOS']}
                                                    label="苹果APP更新地址">
                                                    <Input style={{ width: 360 }} />
                                                </Form.Item>
                                            </Input.Group>
                                        </Form.Item>
                                    </TabPane>
                                    <TabPane tab="腾讯云设置" key="3">
                                        <Form.Item
                                            name={['model', 'tencentCloud_SecretId']}
                                            label="SecretId">
                                            <Input />
                                        </Form.Item>
                                        <Form.Item
                                            name={['model', 'tencentCloud_SecretKey']}
                                            label="SecretKey">
                                            <Input.Password />
                                        </Form.Item>
                                        <Form.Item
                                            name={['model', 'tencentCloud_SmsSdkAppid']}
                                            label="SDKAppId">
                                            <Input />
                                        </Form.Item>
                                        <Form.Item
                                            name={['model', 'tencentCloud_Sms_Sign']}
                                            label="SMS签名">
                                            <Input />
                                        </Form.Item>
                                        <Form.Item
                                            name={['model', 'tencentCloud_Sms_WebProxy']}
                                            label="SMS代理服务器">
                                            <Input />
                                        </Form.Item>
                                        <Form.Item
                                            name={['model', 'tencentCloud_Sms_Endpoint']}
                                            label="SMS指定域名">
                                            <Input />
                                        </Form.Item>
                                    </TabPane>
                                    <TabPane tab="发放二维码更正">
                                        <div style={{marginBottom:"30px"}}>
                                        <Button onClick={this.handleCode} size="large" danger>
                                            校正
                                        </Button>
                                        </div>
                                    </TabPane>

                                </Tabs>
                                <Space>
                                    <Button
                                        type="primary"
                                        htmlType="submit"
                                        loading={this.state.loading}
                                    >保存</Button>
                                </Space>
                            </Form>
                        </Card>
                    </Col>
                </Row>
            </PageHeaderWrapper>
        )
    }
}

export default connect(({ Abp }: API.IConnectState) => ({
    response: Abp.response,
    currentUser: Abp.currentUser,
}))(ViewPage);